package com.lzy.chat.c_service;

import com.lzy.chat.a_entity.AddFriend;
import com.lzy.chat.a_entity.Friend;
import com.lzy.chat.b_mapper.FriendMapper;
import com.lzy.chat.c_service.Exception.Exceptions;
import com.lzy.chat.c_service.Exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {

    private FriendMapper friendMapper;

    @Autowired
    public void setFriendMapper(FriendMapper friendMapper) {
        this.friendMapper = friendMapper;
    }

    @Override
    public void addFriend(Integer userId, Integer friendId) {
        Friend f = friendMapper.findFriend(userId, friendId);
        if (f != null) throw new ServiceException(Exceptions.FriendDuplicatedException);
        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        Integer res = friendMapper.insertFriend(friend);
        if (res != 1) throw new ServiceException(Exceptions.FriendInsertException);
    }

    @Override
    public void deleteFriend(Integer userId, Integer friendId) {
        Friend friend = friendMapper.findFriend(userId, friendId);
        if (friend == null) throw new ServiceException(Exceptions.FriendNotFoundException);
        Integer res = friendMapper.deleteFriend(friend.getId());
        if (res != 1) throw new ServiceException(Exceptions.FriendDeleteException);
    }

    @Override
    public List<Friend> getFriendListByUserId(Integer userId) {
        return friendMapper.findFriendByUserId(userId);
    }

    @Override
    public boolean isFriend(Integer userId_1, Integer userId_2) {
        Friend friend = friendMapper.findFriend(userId_1, userId_2);
        return friend != null;
    }

    @Override
    public void requestToAddFriend(AddFriend addFriend) {
        //添加自己为好友
        if (addFriend.getFromId().equals(addFriend.getToId()))
            throw new ServiceException(Exceptions.FriendCanNotAddSelf);
        //重复好友请求信息
        AddFriend r = friendMapper.getByFromAndTo(addFriend.getFromId(), addFriend.getToId());
        if (r != null) throw new ServiceException(Exceptions.AddFriendDuplicateException);
        Friend friend = friendMapper.findFriend(addFriend.getFromId(), addFriend.getToId());
        //重复好友
        if (friend != null) throw new ServiceException(Exceptions.FriendDuplicatedException);
        addFriend.setIsDeal(0);
        Integer res = friendMapper.insertAddFriend(addFriend);
        if (res != 1) throw new ServiceException(Exceptions.AddFriendInsertException);
    }

    @Override
    public void dealAddFriend(Integer id, Integer deal) {
        Integer res = friendMapper.dealAddFriend(deal, id);
        if (res != 1) throw new ServiceException(Exceptions.AddFriendUpdateException);
    }

    @Override
    public Integer countUnDeal(Integer uid) {
        return friendMapper.countUnDeal(uid);
    }

    @Override
    public List<AddFriend> getUnDealByUid(Integer uid) {
        return friendMapper.getUnDealByUid(uid);
    }
}
